package com.epam.giorgadze.task3.parsers;

import com.epam.giorgadze.task3.countries.Country;
import com.epam.giorgadze.task3.entity.AirCompany;
import com.epam.giorgadze.task3.entity.Manufacturer;
import com.epam.giorgadze.task3.entity.PlaneModel;
import com.epam.giorgadze.task3.entity.plane_type.CargoPlane;
import com.epam.giorgadze.task3.entity.plane_type.PassengerPlane;
import com.epam.giorgadze.task3.entity.plane_type.Plane;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

public class AirCompanyDOMParser {

    public static Element getChild(Element parent, String childName) {
        NodeList nodeList = parent.getElementsByTagName(childName);
        Element child = (Element) nodeList.item(0);
        return child;
    }

    public static String getChildValue(Element parent, String childName) {
        Element child = getChild(parent, childName);
        Node node = child.getFirstChild();
        String value = node.getNodeValue();
        return value;
    }


    public static AirCompany listBuilder(Element root) {
        AirCompany airCompany = new AirCompany();
        Manufacturer manufacturer = null;
        PlaneModel planeModel = null;
        Plane plane = null;
        List<Plane> planeList = new ArrayList<Plane>();

        NodeList airCompanyNodes = root.getElementsByTagName("plane");
        NodeList airCompanyIDNodes = root.getElementsByTagName("airCompany");
        for (int i = 0; i < airCompanyIDNodes.getLength(); i++) {
            airCompany.setAirCompanyID(getChildValue((Element) airCompanyIDNodes.item(i), "airCompanyID"));
        }

        for (int i = 0; i < airCompanyNodes.getLength(); i++) {
            Element planeElement = (Element) airCompanyNodes.item(i);
//            Element  = getChild(airCompanyElement, "plane");
            Element planeModelElement = getChild(planeElement, "planemodel");
            Element manufacturerElement = getChild(planeModelElement, "manufacturer");
            planeModel = new PlaneModel();
            manufacturer = new Manufacturer();
            if (planeElement.getAttribute("xsi:type").equals("CargoPlane")) {
                plane = new CargoPlane();
            } else if (planeElement.getAttribute("xsi:type").equals("PassengerPlane")) {
                plane = new PassengerPlane();
            }
//            airCompany.setAirCompanyID(getChildValue(airCompanyElement, "airCompanyID"));
//            airCompany.setAirCompanyName(getChildValue(airCompanyElement, "airCompanyName"));
            manufacturer.setName(getChildValue(manufacturerElement, "name"));
            manufacturer.setPhoneNumber(Integer.valueOf(getChildValue(manufacturerElement, "phoneNumber")));
            manufacturer.setAddress(getChildValue(manufacturerElement, "address"));
            manufacturer.setEmail(getChildValue(manufacturerElement, "email"));
            manufacturer.setCountry(Country.valueOf(getChildValue(manufacturerElement, "country")));
            planeModel.setName(planeModelElement.getAttribute("planeModelName"));
            planeModel.setFlightLevel(Double.valueOf(getChildValue(planeModelElement, "engineCapacity")));
            planeModel.setCourseSpeed(Double.valueOf(getChildValue(planeModelElement, "courseSpeed")));
            planeModel.setPlaneLength(Double.valueOf(getChildValue(planeModelElement, "planeLength")));
            planeModel.setEngineCapacity(Double.valueOf(getChildValue(planeModelElement, "engineCapacity")));
            planeModel.setPlaneLength(Double.valueOf(getChildValue(planeModelElement, "planeLength")));
            planeModel.setManufacturer(manufacturer);
            plane.setModel(planeModel);
            plane.setLifespan(Double.valueOf(getChildValue(planeElement, "lifespan")));
            plane.setPrice(Double.valueOf(getChildValue(planeElement, "price")));
            plane.setFuelConsumption(Double.valueOf(getChildValue(planeElement, "fuelConsumption")));
            plane.setTravelingDistance(Double.valueOf(getChildValue(planeElement, "travelingDistance")));
            if (plane instanceof CargoPlane)
                ((CargoPlane) plane).setTonnage(Double.valueOf(getChildValue(planeElement, "tonnage")));
            if (plane instanceof PassengerPlane)
                ((PassengerPlane) plane).setNumberOfSeats(Integer.valueOf(getChildValue(planeElement, "numberOfSeats")));
            planeList.add(plane);
        }
        airCompany.setPlanes(planeList);
        return airCompany;
    }
}

